package handler;

/**
 * A database Schema of the Files Table.  This allows
 * a row from the Table to taken from the database and
 * stored onto the active program.
 * 
 * @author David Hietpas
 */
public class NotebookFile {
    private int id;
    private String name;
    private byte[] zip;
    private int owner;
    private Integer lockedBy;
    
    public NotebookFile() {}
    
    public NotebookFile(String name, byte[] zip, int owner, int lockedBy) {
        this.name = name;
        this.zip = zip;
        this.owner = owner;
        if(lockedBy == -1)
            this.lockedBy = -1;
        else
            this.lockedBy = new Integer(lockedBy);
    }
    
    public NotebookFile(int id, String name, byte[] zip, int owner, int lockedBy) {
        this.id = id;
        this.name = name;
        this.zip = zip;
        this.owner = owner;
        if(lockedBy == -1)
            this.lockedBy = -1;
        else
            this.lockedBy = new Integer(lockedBy);
    }
    
    
    /**
     * Returns NotebookFiles file name.
     * 
     * @return String name
     */
    public String getNotebookFileName() {
        return this.name;
    }
    
    /**
     * Returns NotebookFiles file id.
     * 
     * @return int id
     */
    public int getId() {
        return this.id;
    }
    
    /**
     * Returns NotebookFiles Compressed/Encrypted Zip File.
     * 
     * @return byte[] encryptedZip
     */
    public byte[] getNotebookFile() {
        return this.zip;
    }
    
    
    /**
     * Returns the UserID of which user has the file locked.
     * 
     * @return int i
     */
    public int getLockedByUserId() {
        return this.lockedBy;
    }
    
    
    /**
     * Returns NotebookFiles owners user id
     * 
     * @return int id
     */
    public int getOwner() {
        return this.owner;
    }
    
    /**
     * Sets the NotebookFile file name
     * 
     * @param String name
     */
    public void setNotebookFileName(String name) {
        this.name = name;
    }
     
    /**
     * Sets the NotebookFile Compressed/Encrypted file
     * 
     * @param byte[] zip
     */
    public void setNotebookFile(byte[] zip) {
        this.zip = zip;
    }
    
    /**
     * Sets the NotebookFile owner of the file.  The User id
     * of the owner.
     * 
     * @param int id
     */
    public void setOwner(int owner) {
        this.owner = owner;
    }
    
    /**
     * Sets the NotebookFile lock.  The User id
     * is need to lock the file.
     * 
     * @param int id
     */
    public void Lock(int lockedBy) {
        this.lockedBy = new Integer(lockedBy);;
    }
    
    /**
     * Unlocks the NotebookFile.  Nothing is needed to be 
     * passed, it is taken care of automatically.
     */
    public void Unlock() {
        this.lockedBy = -1;
    }
    
    /**
     * If NotebookFile exists in the Database, this method can be called
     * which will update it in the Database.
     */
    public void update() {
        DBHandler dbh = new DBHandler();
        dbh.updateNotebookFile(this);
    }

    /**
     * Returns a String Representation of the NotebookFile Class.
     * 
     * @return String NotebookFile
     */
    public String toString() {
        return "{" + id + " , " + name + " , " + new String(zip) + " , " + owner + " , " + lockedBy + "}";
    }
}